//
//  ViewController.m
//  UI15-Homework
//
//  Created by dllo on 16/8/3.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "CustomCollectionViewLayout.h"
#import "CustomCollectionViewCell.h"
#import "RootClass.h"
#import "Tlist.h"
#define kCollectionViewCellReuse @"reuse"
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, CustomLayoutDelegate>
@property (nonatomic, strong) UICollectionView *myCollectionView;
@property (nonatomic, strong) RootClass *rootClass;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = YES;
    [self handleText];
    [self creatCollectionView];
    
}

- (void)handleText {
    NSURL *url = [NSURL URLWithString:@"http://c.m.163.com/nc/topicset/android/subscribe/manage/listspecial.html"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"GET";
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.rootClass = [RootClass modelWithDic:dic];
    
        [_myCollectionView reloadData];
    }];
    [dataTask resume];

}

- (void)creatCollectionView {
    CustomCollectionViewLayout *flowLayout = [[CustomCollectionViewLayout alloc] init];
    flowLayout.delegate = self;
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 50) collectionViewLayout:flowLayout];
    _myCollectionView.showsHorizontalScrollIndicator = NO;
    _myCollectionView.backgroundColor = [UIColor whiteColor];
    _myCollectionView.delegate = self;
    _myCollectionView.dataSource = self;
    [self.view addSubview:_myCollectionView];
    [_myCollectionView registerNib:[UINib nibWithNibName:@"CustomCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kCollectionViewCellReuse];

}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout height:(CGFloat)height widthForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *str = [_rootClass.tList[indexPath.row] tname];
    CGRect rect = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];
    CGFloat width = ceil(rect.size.width);
    return width;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _rootClass.tList.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellReuse forIndexPath:indexPath];
    cell.titleLabel.textAlignment = NSTextAlignmentCenter;
    cell.titleLabel.text = [_rootClass.tList[indexPath.row] tname];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
